import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.omg.PortableServer.THREAD_POLICY_ID;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadPoolDemo {

    public static void main(String[] args) {

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
                namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(i + "");
        }
        CountDownLatch countDownLatch = new CountDownLatch(list.size());

        long t1 = System.currentTimeMillis();
        list.forEach(x -> {
            System.out.println(x);
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Long t2 = System.currentTimeMillis();
        list.forEach(x -> {

            executor.submit(() -> {
                System.out.println(x);
                Long t4 = System.currentTimeMillis();
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
        });
        try {
            countDownLatch.await();
            executor.shutdown();

            Long t3 = System.currentTimeMillis();
            System.out.println("okkk" + (t2 - t1));
            System.out.println("ok" + (t3 - t2));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
